import axios from 'axios';
import type { OssConfig, OssFile, UploadOptions } from '../types/oss';

const api = axios.create({
  baseURL: '/api',
  timeout: 30000
});

export const ossApi = {
  // 切换 OSS 提供商
  switchProvider(config: OssConfig) {
    return api.post('/oss/switch', config);
  },

  // 上传文件
  uploadFile(file: File, options: UploadOptions) {
    const formData = new FormData();
    formData.append('file', file);
    formData.append('isPublic', String(options.isPublic));
    if (options.path) {
      formData.append('path', options.path);
    }
    return api.post<OssFile>('/oss/upload', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    });
  },

  // 获取文件列表
  getFiles() {
    return api.get<OssFile[]>('/oss/files');
  },

  // 删除文件
  deleteFile(path: string) {
    return api.delete(`/oss/files/${encodeURIComponent(path)}`);
  }
};